package com.yekaile.carsystem.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yekaile.carsystem.entity.Goods;
import com.yekaile.carsystem.entity.GoodsType;
import com.yekaile.carsystem.service.GoodsService;
import com.yekaile.carsystem.util.Result;
import lombok.AllArgsConstructor;
import org.apache.http.HttpStatus;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

/**
 * @author yekaile
 */
@RestController
@RequestMapping("goods")
@AllArgsConstructor(onConstructor_=@Autowired)

public class GoodsController {

    GoodsService goodsService;

    HttpServletRequest request;




    @GetMapping("getGoods")
    @RequiresPermissions("goods:select")
    public Result<PageInfo<Goods>> listGoods(Goods goods, @RequestParam(defaultValue = "1", required = false) Integer pageNum,
                                             @RequestParam(defaultValue = "8", required = false) Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Goods> goodsList = goodsService.getGoods(goods);
        PageInfo<Goods> pageInfo = new PageInfo<>(goodsList);
        return new Result<>(HttpStatus.SC_OK, "success", pageInfo);
    }

    @PutMapping("doGoods")
    @RequiresPermissions("goods:update")
    public Result<String> doGoods(Integer goodsId, Integer isDeleted) {
        goodsService.doGoods(goodsId, isDeleted);
        return new Result<>(HttpStatus.SC_CREATED, "success");
    }


    @PostMapping("addGoods")
    @RequiresPermissions("goods:add")
    public Result<String> addGoods(@RequestBody Goods goods) {
        goodsService.addGoods(goods);
        return new Result<>(HttpStatus.SC_CREATED, "添加成功");
    }

    @GetMapping("getGoodsType")
    @RequiresPermissions("goods:select")
    public Result<List<GoodsType>> goodsType(Integer goodsTypeId) {
        return new Result<>(HttpStatus.SC_OK, "success", goodsService.listGoodsType(goodsTypeId));
    }

    @PutMapping("doGoodsType")
    @RequiresPermissions("goods:update")
    public Result<String> doGoodsType(Integer goodsTypeId, Integer isDeleted) {
        goodsService.doGoodsType(goodsTypeId, isDeleted);
        return new Result<>(HttpStatus.SC_CREATED, "success");
    }


    @PutMapping("updateGoods")
    @RequiresPermissions("goods:update")
    public Result<String> updateGoods(@RequestBody Goods goods) {
        goodsService.updateGoods(goods);
        return new Result<>(HttpStatus.SC_CREATED, "success");
    }

    @PostMapping("addGoodsType")
    @RequiresPermissions("goods:add")
    public Result<String> addGoodsType(@RequestBody GoodsType goodsType) {
        goodsService.addGoodsType(goodsType);
        return new Result<>(HttpStatus.SC_CREATED, "success");
    }


    @PutMapping("updateGoodsType")
    @RequiresPermissions("goods:update")
    public Result<String> updateGoodsType(@RequestBody GoodsType goodsType) {
        goodsService.updateGoodsType(goodsType);
        return new Result<>(HttpStatus.SC_CREATED, "success");
    }


}
